package com.link.base.base.crowd.service;

import com.link.base.base.crowd.dao.mybatis.mapper.CrowdAcctMapper;
import com.link.base.base.crowd.model.Crowd;
import com.link.base.base.crowd.model.CrowdAcct;
import com.link.base.wechat.wxplatform.model.WxPlatform;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceException;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.util.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 人群-消费者中间表
 *
 * @author 何启谦
 * @date 2019-02-21
 */
@Service
public class CrowdAcctServiceImpl extends BasicServiceImpl<CrowdAcct> implements CrowdAcctService {

    @Resource
    private CrowdAcctMapper crowdAcctMapper;
    @Resource
    private CrowdService crowdService;

    @Override
    public BasicMapper<CrowdAcct> getBasicMapper() {
        return crowdAcctMapper;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void batchInsert(Long crowdId, List<String> acctIds) throws Exception {
        if (null == crowdId) {
            throw new BasicServiceException("crowdId 不能为空");
        }
        if (acctIds == null) {
            throw new BasicServiceException("acctIds 不能为空");
        }
        Crowd crowd = crowdService.queryById(crowdId);
        List<CrowdAcct> crowdAcctList = new ArrayList<CrowdAcct>();
        for (String acctId : acctIds) {
            if (StringUtils.isNull(acctId)) {
                continue;
            }
            CrowdAcct crowdAcct = new CrowdAcct();
            crowdAcct.setAcctId(acctId);
            crowdAcct.setCrowdId(crowdId);
            crowdAcct.setId(keyGenerateService.keyGenerate());
            crowdAcct.setCreatedBy(crowd.getCreatedBy());
            crowdAcct.setLastUpdatedBy(crowd.getLastUpdatedBy());
            crowdAcctList.add(crowdAcct);
        }
        crowdAcctMapper.batchInsertAcct(crowdAcctList);
    }

    @Override
    public Integer queryCountByCrowdId(Long crowdId, boolean openIdFlag, boolean phoneFlag) throws Exception {
        Integer consumerCount = 0;
        CrowdAcct crowdAcct = new CrowdAcct();
        crowdAcct.setCrowdId(crowdId);
        if (openIdFlag && !phoneFlag) {
            consumerCount = crowdAcctMapper.queryCountByCrowdIdForOpenId(crowdAcct);
        } else if (!openIdFlag && phoneFlag) {
            consumerCount = crowdAcctMapper.queryCountByCrowdIdForPhone(crowdAcct);
        } else {
            consumerCount = crowdAcctMapper.queryCountByCrowdIdForOpenIdAndPhone(crowdAcct);
        }
        // 返回总数
        return consumerCount;
    }

    @Override
    public List<CrowdAcct> queryByCount(CrowdAcct crowdAcct) throws Exception {
        return crowdAcctMapper.queryByCount(crowdAcct);
    }

    @Override
    public List<String> queryAccntPhone(CrowdAcct crowdAcct) throws Exception {
        return crowdAcctMapper.queryAccntPhone(crowdAcct);
    }

    @Override
    public List<String> queryAccntOpenId(CrowdAcct crowdAcct) throws Exception {
        return crowdAcctMapper.queryAccntOpenId(crowdAcct);
    }

    @Override
    @Transactional(rollbackFor = BasicServiceException.class)
    public CrowdAcct queryCrowdAcctFans(CrowdAcct entity) throws Exception {
        Long crowdId = entity.getCrowdId();
        if (crowdId == null) {
            throw new BasicServiceException("人群包id不能为空");
        }
        List<WxPlatform> platforms = entity.getWxPlatforms();
        if ((platforms != null && platforms.size() == 0) || platforms == null) {
            //查询有效粉丝数量
            int fansNumber = crowdAcctMapper.queryCount(entity);
            entity.setFansNumber(fansNumber);
        } else if (platforms != null && platforms.size() > 0) {
            //查询有效粉丝数量
            for (WxPlatform wxPlatform : platforms) {
                entity.setAppId(wxPlatform.getWx_app_id());
                int fansNumber = crowdAcctMapper.queryCount(entity);
                entity.setFansNumber(fansNumber);
            }
        }
        // 查询所有人群包下公众号以及有效粉丝数量
        List<WxPlatform> wxPlatformList = crowdAcctMapper.queryWxPlatformByCrowdId(entity);
        entity.setWxPlatforms(wxPlatformList);
        return entity;
    }

    @Override
    public List<CrowdAcct> queryAllAccountByCrowdId(CrowdAcct entity) throws Exception {
        return crowdAcctMapper.queryAllAccountByCrowdId(entity);
    }

    @Override
    public List<CrowdAcct> queryOpenids(CrowdAcct crowdAcct) throws Exception {
        return crowdAcctMapper.queryOpenids(crowdAcct);
    }
}

